package websocker;

/**
 * Created by IPE on 2016/12/27.
 */
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.servlet.http.HttpSession;

public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor{

    @Autowired
    private HttpSession session;

    @Override
    public boolean beforeHandshake(ServerHttpRequest request,
                                   ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {
        System.out.println("连接"+session.getAttribute("username"));
        attributes.put("username",session.getAttribute("username"));
        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    @Override
    public void afterHandshake(ServerHttpRequest request,
                               ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception ex) {
        //System.out.println("After Handshake");
        super.afterHandshake(request, response, wsHandler, ex);
    }

}
